import cv2
import matplotlib.pyplot as plt

img = cv2.imread("./img/cat.png")
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur_img = gray_img.copy()
cv2.blur(gray_img,(3,3),blur_img)

# 阈值处理方法
ret,thresh1 = cv2.threshold(blur_img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(blur_img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(blur_img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(blur_img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(blur_img,127,255,cv2.THRESH_TOZERO_INV)

titles = ['img','THRESH_BINARY','THRESH_BINARY_INV','THRESH_TRUNC','THRESH_TOZERO','THRESH_TOZERO_INV']
images = [img,thresh1,thresh2,thresh3,thresh4,thresh5]
for i in range(6):
    plt.subplot(2, 3,i+1)
    # 通过图表形式展示图像。
    plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.show()
cv2.waitKey(0)
